import { getDicList, admdvsList, placemedlist, orgList } from '@/api/modules/user';
import { getRoleSelectList, getMenuSelectList } from '@/api/modules/sys';
import { GlobalStore } from '@/stores';
import { sysStaticData } from '@/utils/serviceDict';
import { transformToTreeData, buildMenuTreeRecursive } from '@/utils/util';
export const useFilterHandle = () => {
	const globalStore = GlobalStore();
	// 日期转换
	const dateFilter = (value: any, dateType: number = 1) => {
		if (!value) {
			return '--';
		}
		const date: Date = new Date(value);
		const YY: string = `${date.getFullYear()}-`;
		const MM: string =
			(date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + (dateType === 3 ? '' : '-');
		const DD: string | number = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
		const hh: string | number = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
		const mm: string | number = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
		const ss: string | number = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
		return dateType === 1 ? YY + MM + DD + ' ' + hh + mm + ss : dateType === 2 ? YY + MM + DD : YY + MM;
	};

	// 百分比转换,保留两位小数
	const rateFilter = (value: any, num: number = 100): string => {
		if (!value) {
			return '0';
		}
		return `${(value * num).toFixed(2)}%`;
	};

	/**
	 * 获取字典数据并转换
	 * @param dicTypeCode 字典参数
	 */
	const getDicData = (dicTypeCode?: string): Promise<any> => {
		return new Promise(async resolve => {
			if (globalStore.dictData && globalStore.dictData.length > 0) {
				if (dicTypeCode) {
					const data = globalStore.dictData.filter((item: any) => {
						return item.dicTypeCode === dicTypeCode;
					});
					resolve({
						data: data.sort((a: any, b: any) => a.orderNum - b.orderNum)
					});
				} else {
					resolve({ data: globalStore.dictData });
				}
				return;
			}
			const { data } = await getDicList({ pageNum: 1, pageSize: 2000 });
			const dictArr: any = data?.map((item: any) => {
				return {
					value: item.dicValue,
					label: item.dicName,
					dicTypeCode: item.dicType,
					orderNum: item.orderNum
				};
			});
			const dictList = sysStaticData.concat(dictArr);
			globalStore.setDictData(dictList);
			resolve({ data: dictList });
		});
	};

	/**
	 * 筛选字典值
	 * @param value
	 * @param dicTypeCode
	 * @returns String
	 */
	const filterDic = (dicTypeCode: string, value: string | undefined) => {
		const dicList = globalStore.dictData;
		if (dicList.length > 0) {
			if (value) {
				const filterDic = dicList.filter((item: any) => {
					return item.dicTypeCode === dicTypeCode;
				});
				const findVal = filterDic.find((item: any) => {
					return value === item.value;
				});
				return findVal?.label;
			} else {
				return '--';
			}
		} else {
			return '--';
		}
	};

	// 获取角色选择列表
	const getRoleList = async () => {
		return new Promise(async resolve => {
			const { list } = await getRoleSelectList();
			const data = list.map((item: any) => {
				return {
					value: item.roleId,
					label: item.roleName
				};
			});
			resolve({ data });
		});
	};

	// 获取菜单选择列表
	const getSelectMenuList = async () => {
		return new Promise(async resolve => {
			const { menuList } = await getMenuSelectList();
			const data = menuList.map((item: any) => {
				return {
					...item,
					value: item.menuId,
					label: item.name
				};
			});
			const list = buildMenuTreeRecursive(data, -1);
			console.log(menuList, list);
			resolve({ data: list });
		});
	};

	// 获取医保区划
	const getAdmdvsList = async (level: number = 3) => {
		return new Promise(async resolve => {
			if (globalStore.admdvsList && globalStore.admdvsList.length > 0) {
				const data = transformToTreeData(globalStore.admdvsList, level);
				resolve({ data });
				return;
			}
			const { data } = await admdvsList();
			const treeData = transformToTreeData(data, level);
			globalStore.setAdmdv(data);
			resolve({ data: treeData });
		});
	};

	// 转义医保区划
	const getAdmdvs = (admdvs: string) => {
		const admdvsList = globalStore.admdvsList;
		const admdvsName = admdvsList?.find((item: any) => item.admdvs === admdvs)?.admdvsName;
		return admdvsName ?? '--';
	};

	// 获取省级数据
	const getProvinceList = async () => {
		return new Promise(async resolve => {
			const { data }: any = await getAdmdvsList(1);
			resolve({ data });
		});
	};

	// 获取市级数据
	const getCityList = async () => {
		return new Promise(async resolve => {
			const { data }: any = await getAdmdvsList(2);
			resolve({ data });
		});
	};

	// 获取机构
	const getOrgList = async () => {
		return new Promise(async resolve => {
			if (globalStore.orgList && globalStore.orgList.length > 0) {
				resolve({ data: globalStore.orgList });
				return;
			}
			const { data } = await orgList();
			globalStore.setOrgList(data);
			resolve({ data });
		});
	};

	// 获取机构过滤列表
	const getOrgFilterList = async () => {
		return new Promise(async resolve => {
			if (globalStore.orgFilterList && globalStore.orgFilterList.length > 0) {
				resolve({ data: globalStore.orgFilterList });
				return;
			}
			const { data } = await orgList('1', '1');
			globalStore.setOrgFilterList(data);
			resolve({ data });
		});
	};

	// 获取上级目录
	const getPlacemedList = async (admdvs: string) => {
		return new Promise(async resolve => {
			const { data } = await placemedlist(admdvs);
			resolve({ data });
		});
	};
	return {
		dateFilter,
		rateFilter,
		getDicData,
		getAdmdvsList,
		getPlacemedList,
		filterDic,
		getOrgList,
		getOrgFilterList,
		getProvinceList,
		getCityList,
		getAdmdvs,
		getRoleList,
		getSelectMenuList
	};
};
